package lv.bond.science.nnstudio.ui.netsetup;

import lv.bond.science.nnstudio.core.MlpModel;
import lv.bond.science.nnstudio.core.NetworkParams;
import lv.bond.science.nnstudio.model.ImagesBean;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class NetSetupView extends ViewPart {

	public static final String ID = "lv.bond.science.nnstudio.ui.netsetup.NetSetupView"; 
	// Needs to be whatever is mentioned in plugin.xml  //  @jve:decl-index=0:

	private Composite top = null;
	private Label xDimLabel = null;
	private Text xDimText = null;
	private Label yDimLabel = null;
	private Text yDimText = null;
	private Label hiddenDimLabel = null;
	private Text hiddenText = null;
	private Label outputDimLabel = null;
	private Text outputDimText = null;
	private Label leranRateLabel = null;
	private Label learnDecreaseRateLabel = null;
	private Label sigmoidStepCoefLabel = null;
	private Text learnRateText = null;
	private Text learnDecreaseCoefText = null;
	private Text sigmoidStepCoefText = null;
	private Button createAndTrainbutton = null;
	private Button testButton = null;
	
	private MlpModel mlpModel = null;  //  @jve:decl-index=0:

	private Label epochsCountLabel = null;

	private Text epochsCountText = null;

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		GridData gridData22 = new GridData();
		gridData22.horizontalSpan = 3;
		gridData22.verticalAlignment = GridData.CENTER;
		gridData22.horizontalAlignment = GridData.FILL;
		GridData gridData12 = new GridData();
		gridData12.horizontalAlignment = GridData.END;
		gridData12.verticalAlignment = GridData.CENTER;
		GridData gridData15 = new GridData();
		gridData15.verticalAlignment = GridData.CENTER;
		gridData15.horizontalSpan = 3;
		gridData15.horizontalAlignment = GridData.FILL;
		GridData gridData111 = new GridData();
		gridData111.horizontalSpan = 3;
		gridData111.verticalAlignment = GridData.CENTER;
		gridData111.horizontalAlignment = GridData.FILL;
		GridData gridData10 = new GridData();
		gridData10.horizontalSpan = 3;
		gridData10.verticalAlignment = GridData.CENTER;
		gridData10.horizontalAlignment = GridData.FILL;
		GridData gridData9 = new GridData();
		gridData9.horizontalSpan = 3;
		gridData9.verticalAlignment = GridData.CENTER;
		gridData9.horizontalAlignment = GridData.FILL;
		GridData gridData8 = new GridData();
		gridData8.horizontalSpan = 3;
		gridData8.verticalAlignment = GridData.CENTER;
		gridData8.horizontalAlignment = GridData.FILL;
		GridData gridData7 = new GridData();
		gridData7.horizontalAlignment = GridData.END;
		gridData7.verticalAlignment = GridData.CENTER;
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.END;
		gridData6.verticalAlignment = GridData.CENTER;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.END;
		gridData5.verticalAlignment = GridData.CENTER;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.END;
		gridData4.verticalAlignment = GridData.CENTER;
		GridData gridData31 = new GridData();
		gridData31.horizontalAlignment = GridData.END;
		gridData31.verticalAlignment = GridData.CENTER;
		GridData gridData21 = new GridData();
		gridData21.horizontalSpan = 3;
		gridData21.verticalAlignment = GridData.CENTER;
		gridData21.horizontalAlignment = GridData.FILL;
		GridData gridData11 = new GridData();
		gridData11.widthHint = 30;
		GridData gridData3 = new GridData();
		gridData3.horizontalSpan = 3;
		gridData3.verticalAlignment = GridData.CENTER;
		gridData3.horizontalAlignment = GridData.FILL;
		GridData gridData2 = new GridData();
		gridData2.horizontalIndent = 10;
		gridData2.verticalAlignment = GridData.CENTER;
		gridData2.horizontalAlignment = GridData.END;
		GridData gridData1 = new GridData();
		gridData1.horizontalIndent = 0;
		gridData1.widthHint = 30;
		GridData gridData = new GridData();
		gridData.horizontalIndent = 10;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 4;
		gridLayout.marginWidth = 5;
		gridLayout.verticalSpacing = 5;
		gridLayout.marginHeight = 5;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		xDimLabel = new Label(top, SWT.NONE);
		xDimLabel.setText("X");
		xDimLabel.setLayoutData(gridData2);
		xDimText = new Text(top, SWT.BORDER);
		xDimText.setEditable(false);
		xDimText.setEnabled(false);
		xDimText.setLayoutData(gridData1);
		yDimLabel = new Label(top, SWT.NONE);
		yDimLabel.setText("Y");
		yDimLabel.setLayoutData(gridData);
		yDimText = new Text(top, SWT.BORDER);
		yDimText.setEditable(false);
		yDimText.setEnabled(false);
		yDimText.setLayoutData(gridData11);
		hiddenDimLabel = new Label(top, SWT.NONE);
		hiddenDimLabel.setText("Hidden Layer");
		hiddenDimLabel.setLayoutData(gridData31);
		hiddenText = new Text(top, SWT.BORDER);
		hiddenText.setLayoutData(gridData3);
		outputDimLabel = new Label(top, SWT.NONE);
		outputDimLabel.setText("Output Layer");
		outputDimLabel.setLayoutData(gridData4);
		outputDimText = new Text(top, SWT.BORDER);
		outputDimText.setEditable(false);
		outputDimText.setEnabled(false);
		outputDimText.setLayoutData(gridData21);
		leranRateLabel = new Label(top, SWT.NONE);
		leranRateLabel.setText("Learn Rate");
		leranRateLabel.setLayoutData(gridData5);
		learnRateText = new Text(top, SWT.BORDER);
		learnRateText.setText("0.25");
		learnRateText.setLayoutData(gridData8);
		learnDecreaseRateLabel = new Label(top, SWT.NONE);
		learnDecreaseRateLabel.setText("Learn Decrease");
		learnDecreaseRateLabel.setLayoutData(gridData6);
		learnDecreaseCoefText = new Text(top, SWT.BORDER);
		learnDecreaseCoefText.setText("1.0");
		learnDecreaseCoefText.setLayoutData(gridData9);
		sigmoidStepCoefLabel = new Label(top, SWT.NONE);
		sigmoidStepCoefLabel.setText("Sigmoid Step Coef");
		sigmoidStepCoefLabel.setLayoutData(gridData7);
		sigmoidStepCoefText = new Text(top, SWT.BORDER);
		sigmoidStepCoefText.setText("1.0");
		sigmoidStepCoefText.setLayoutData(gridData10);
		epochsCountLabel = new Label(top, SWT.NONE);
		epochsCountLabel.setText("Epochs Count");
		epochsCountLabel.setLayoutData(gridData12);
		epochsCountText = new Text(top, SWT.BORDER);
		epochsCountText.setText("150");
		epochsCountText.setLayoutData(gridData22);
		Label filler12 = new Label(top, SWT.NONE);
		createAndTrainbutton = new Button(top, SWT.NONE);
		createAndTrainbutton.setText("Create and Train");
		createAndTrainbutton.setLayoutData(gridData111);
		createAndTrainbutton
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
						actionCreateAndRun();
					}
				});
		Label filler13 = new Label(top, SWT.NONE);
		testButton = new Button(top, SWT.NONE);
		testButton.setText("Test");
		testButton.setLayoutData(gridData15);
		testButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				actionTest();
			}
		});
	}

	@Override
	public void setFocus() {
		this.hiddenText.setFocus();
	}
	
	private void actionCreateAndRun() {
		this.mlpModel = new MlpModel();
		
		NetworkParams params = new NetworkParams();
		
		ImagesBean bean = ImagesBean.getInstance();
		Image trainImage = new Image(null, bean.getFileNameTrainData());
		ImageData trainImageData = trainImage.getImageData();
		
		params.setInputDimX(trainImageData.height);
		params.setInputDimY(trainImageData.height);
		
		trainImage = new Image(null, bean.getFileNameTrainTargetData());
		trainImageData = trainImage.getImageData();
		params.setOutputDim(trainImageData.width);
		
		
		params.setHiddenDim(
				Integer.valueOf(this.hiddenText.getText()));
		params.setLearnRate(
				Double.valueOf(this.learnRateText.getText()));
		params.setLearnRateDecrease(
				Double.valueOf(this.learnDecreaseCoefText.getText()));
		params.setSigmoidStep(
				Double.valueOf(this.sigmoidStepCoefText.getText()));
		params.setEpochsCount(
				Integer.valueOf(this.epochsCountText.getText()));
		
		this.mlpModel.createMlpNet(params);
		
		this.mlpModel.runMlp();
	}

	private void actionTest() {
		this.mlpModel.runTesting();
		
	}

}
